<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avatar_box">
        <img src="../assets/a.jpg" alt>
      </div>

      <el-form ref="loginRef" :model="loginForm" :rules="loginFormRules" class="login_form">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="iconfont icon-3702mima"
            type="password"
          ></el-input>
        </el-form-item>

        <el-row class="btns">
          <el-button type="success" @click="login">飒爽登录</el-button>
          <el-button type="warning" @click="resetLogin">重新输入</el-button>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: "admin",
        password: "123456"
      },
      loginFormRules: {
        username: [
          { required: true, message: "输入用户名", trigger: "blur" },
          { min: 3, max: 9, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "输入密码", trigger: "blur" },
          { min: 3, max: 9, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    resetLogin() {
      console.log(this);
      this.$refs.loginRef.resetFields();
    },
    login() {
      this.$refs.loginRef.validate(async valid => {
        //console.log(valid);
        if (!valid) return;
        // let result=await this.$http.post("login",this.loginForm);
        // console.log(result);
        const { data: res } = await this.$http.post("login", this.loginForm);
        if (res.meta.status !== 200) return this.$message.error("登陆失败");
        this.$message.success("登录成功");
        sessionStorage.setItem("token", res.data.token);
        this.$router.push("/Home");
      });
    }
  }
};
</script>

<style lang="less" scoped>
.login_container {
    background-color: yellowgreen;
  height: 100%;
  
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 30px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid black;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: purple;
    img{
      height: 100%;
      width: 100%;
      border-radius: 50%;
      background-color: rgb(22, 19, 19);
    }
  }

  .login_form {
    position: absolute;
    margin-top: 30px;
    padding: 25px;
    width: 100%;
  }
  .btns {
    display: flex;
    justify-content: space-around;
  }
}

// scoped:样式只在当前内部生效
</style>

